package com.kfm.jdbc.day0406;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);

            // 等待客户端连接, 阻塞方法
            Socket accept = serverSocket.accept();

            // 有客户端连接
            System.out.println("有客户端连接" + accept.getInetAddress().getHostAddress());

            InputStream inputStream = accept.getInputStream();
            byte[] bytes = new byte[1024];
            int length = inputStream.read(bytes);

            System.out.println(new String(bytes, 0, length));

            OutputStream outputStream = accept.getOutputStream();
            outputStream.write("我接收到你的消息了".getBytes());

            outputStream.flush();

            accept.close();

            serverSocket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
